<?php

namespace app\services;

use app\payment\PayFactory;
use plugin\admin\app\model\Order;
use plugin\admin\app\model\OrderGoods;
use plugin\admin\app\model\PrizeDrawActivity;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserPrizeDraw;
use plugin\admin\app\model\UserPropertyLog;
use plugin\admin\app\model\UserTeam;
use support\Log;
use Webman\RedisQueue\Redis;

class ModelService
{
    public function prizeDraw($order)
    {
        try {
            $goodsList = OrderGoods::where('order_no', $order['order_no'])->get()->toArray();

            foreach ($goodsList as $goods) {
                $activity = PrizeDrawService::goodsActivityOriginal($goods['goods_id']);

                if ($activity && $activity['need_buy_count'] <= $goods['count']) {
                    $limit = floor($goods['count'] / $activity['need_buy_count']);

                    for ($i=0; $i<$limit; $i++) {
                        UserPrizeDrawService::add($activity['id'], $order['user_id'], $order['order_no'], $goods['goods_id'], $goods['count']);
                    }

                    PrizeDrawActivity::where('id', $activity['id'])->increment('sale_count', $goods['count']);
                }


            }

        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function bindParent($data)
    {
        try {

            UserService::bindParent($data[0], $data[1]);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function givePoint($data)
    {
        if ($data[1] > 0) {
            $point = ConfigService::get('invite_point');
            PropertyService::add($data[1], PayFactory::BALANCE, $point, UserPropertyLog::INVITE_POINT);
        }
    }

     public function registerGivePoint($data)
    {
        if ($data[0] > 0) {
            $point = ConfigService::get('register_give_point');
            PropertyService::add($data[0], PayFactory::BALANCE, $point, UserPropertyLog::REGISTER_POINT);
        }
    }

    public function toVip($order)
    {
        try {
            UserService::upgradeToVip($order['user_id']);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function reduceIntegral($order)
    {
        try {
            if ($order['integral'] > 0) {
                PropertyService::reduce($order['user_id'], PayFactory::INTEGRAL, $order['integral'], UserPropertyLog::INTEGRAL_PAY_ORDER, '', $order['order_no']);
            }
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }

    }

    public function profitSharing($order)
    {
        try {
            OrderService::rebate($order);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function updateTeamInfo($order)
    {
        try {
            echo 2222222222222;
            UserTeamService::addTeamConsume($order);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function checkCanCreateOrder($orderPreviewInfo)
    {
        $limitCount = ConfigService::get('limit_gift_count');

        if (OrderService::getRangeTimeGiftOrderCount(date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')) + 1 > $limitCount) {
            throw new \Exception('每月仅能下单一次报单产品');
        }
    }
}